package org.groupg.practice.guava;

import java.util.concurrent.locks.ReentrantLock;

public class MutexExample {
    private final ReentrantLock lock = new ReentrantLock();

    public void doSomething() {
        lock.lock();
        try {
            // 临界区代码
            System.out.println(Thread.currentThread().getName() + " 正在执行");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        MutexExample example = new MutexExample();
        for (int i = 0; i < 3; i++) {
            new Thread(example::doSomething).start();
        }
    }
}